
//
//  YLReceivablesViewController.m
//  YLFinanceService
//  Created by she1323 on 2017/9/14.
//  Copyright © 2017年 she1323. All rights reserved.
//  收款信息控制器

#import "YLReceivablesViewController.h"
#import "YLTitleHeader.h"
#import "YLSubTitileNoArrowTableViewCell.h"
#import "YLReceivablesViewModel.h"
#import <MJRefresh.h>
@interface YLReceivablesViewController ()

@property (nonatomic, strong) YLReceivablesViewModel *viewModel;

@property (nonatomic, assign) NSInteger index;

@end

@implementation YLReceivablesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLSubTitileNoArrowTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLSubTitileNoArrowTableViewCell cellID]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.tableView.mj_footer.hidden = YES;
            weakSelf.index = 1;
            [weakSelf loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer.hidden = YES;
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData
{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionaryWithCapacity:4];
    parmas[@"PageIndex"] = @(_index);
    parmas[@"ContractCode"] = _contractCode;
    parmas[@"Status"] = @(_status);
    
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:parmas] subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefreshing];
        [weakSelf.tableView reloadData];
    }];
    
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefreshing];
    }];
    
    [self.viewModel.wh_noMoreDataSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (void)endRefreshing
{
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.mj_footer.hidden = !self.viewModel.groups.count;
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
- (YLReceivablesViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLReceivablesViewModel alloc] init];
    }
    return _viewModel;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YLReceivablesGroup *group = self.viewModel.groups[section];
    return group.models.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.groups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLReceivablesGroup *group = self.viewModel.groups[indexPath.section];
    YLTitleContentModel *model = group.models[indexPath.row];
    YLSubTitileNoArrowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLSubTitileNoArrowTableViewCell cellID]];
    cell.model = model;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YLReceivablesGroup *group = self.viewModel.groups[section];
    
    YLTitleHeader *header = [YLTitleHeader new];
    header.title = group.title;
    header.showCancelStatus = !group.abandoned;
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}



- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * text = @"暂无此收款详情!";
    
    NSDictionary * attributes = @ { NSFontAttributeName:[UIFont boldSystemFontOfSize:19.0f ],
        NSForegroundColorAttributeName:YLLineColor};
    
    return [[ NSAttributedString  alloc ] initWithString:text attributes:attributes];
}


- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 25.0f;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return -30;
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}
@end
